package com.itheima.reflect3;

import java.lang.reflect.Field;

public class MyReflectDemo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        //通过反射获取class对象的成员变量
        //获取class对象
        Class clazz = Class.forName("com.itheima.reflect3.Student");

        //获取所有的成员变量
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        //获取指定的成员变量
        Field age = clazz.getDeclaredField("age");
        System.out.println(age);

        //获取成员变量的修饰符
        int modifiers = age.getModifiers();
        System.out.println(modifiers);
        //获取成员变量的类型
        Class type = age.getType();
        System.out.println(type);
        //获取成员变量的名称
        String name = age.getName();
        System.out.println(name);
        //获取成员变量的值
        Student s = new Student("zhangsan", 20, "男");
        age.setAccessible(true);
        int value = (int) age.get(s);
        System.out.println(value);
    }
}
